package h0904;

import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
  * @description 最常见的单词
  * @author 不知名帅哥
  * @date 2024/9/4 17:07
  * @version 1.0
*/
public class MostCommonWord {
    public static void main(String[] args) {
        String paragraph="Bob hit a ball, the hit BALL flew far after it was hit.";
        String[] banned={"hit"};
        System.out.println(mostCommonWord(paragraph, banned));
    }
    public static String mostCommonWord(String paragraph, String[] banned) {
        Pattern p=Pattern.compile("\\b\\w+\\b");
        Matcher matcher = p.matcher(paragraph);
        HashMap<String, Integer> map = new HashMap<>();
        HashSet<String> set = new HashSet<>(List.of(banned));
        while (matcher.find()) {
            String cur = matcher.group();
            cur=cur.toLowerCase();
            if (!set.contains(cur)){
                map.put(cur, map.getOrDefault(cur,0)+1);
            }
        }
        List<String> list = map.keySet().stream().sorted((o1, o2) -> map.get(o2) - map.get(o1)).toList();
        return list.get(0);
    }
}
